/**
 * 
 */
package who.tmap.toolkit.shape.annotation.impl;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import who.tmap.bo.TextStyle;
import who.tmap.toolkit.EditMode;
import who.tmap.toolkit.shape.LabelShape;
import who.tmap.toolkit.shape.annotation.LabelsListShape;

/**
 * @author stl
 *
 */
public class LabelsListShapeImpl extends BaseTextMapShapeImpl implements LabelsListShape {
	
	private String name;
	private TextStyle style;
	private boolean visible;
	private boolean backgroundTransparent;
	private Map<String, LabelShape> labelShapes;
	
	public LabelsListShapeImpl() {
		super();
		
		pt =new Point2D.Double( 0, 0);
		visible =true;
		
		setType( LABELS_LIST );
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}	

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public TextStyle getStyle() {
		return style;
	}

	public void setStyle(TextStyle style) {
		this.style = style;
		
		setFontName( style.getFontName() );
		setFontSize( style.getFontSize() );
		setFontStyle( style.getFontStyle() );
	}

	public boolean isBackgroundTransparent() {
		return backgroundTransparent;
	}

	public void setBackgroundTransparent(boolean backgroundTransparent) {
		this.backgroundTransparent = backgroundTransparent;
	}
		
	public Map<String, LabelShape> getLabelShapes() {
		return labelShapes;
	}

	public void setLabelShapes(Map<String, LabelShape> labelShapes) {
		this.labelShapes = labelShapes;
	}
	
	public void resetLabelShapes() {
		labelShapes =new TreeMap<String, LabelShape>();
	}

	/**
	 * @see JDialogSetPolygonName.jComboBoxLabelList
	 */
	public String toString() {
		return name;
	}	
	
	public void draw(Graphics g) {
	
		Graphics2D g2d =(Graphics2D)g;
		
		TextRenderingHelper trh =new TextRenderingHelper( getScreenToMapFactor(), getZoomFactor() );
		
		trh.init( g2d, fontName, fontStyle, fontSize );
		
		List<String> labelLines =new ArrayList<String>();
		for( LabelShape labelShape : labelShapes.values() ) {
			labelLines.add( labelShape.getText() );
		}	
		
		// If label list is empty and annotations edition is on,
		// add some comments so the user can edit the label list
		if ( labelLines.size() == 0 && ( getEditMode() & EditMode.ANNOTATIONS ) == EditMode.ANNOTATIONS ) {
			labelLines.add( "Labels list " + getName() );
		}
		
		bounds =trh.prepareTextForRendering( g2d, labelLines, pt, TextRenderingHelper.ADD_MARGINS );
		
		trh.renderText( g2d, backgroundTransparent
						   , getBackgroundColor() );
		
		displaySelectionHandles( g );					
		
	}
	
}
